﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word=Microsoft.Office.Interop.Word;

namespace MyWordAddIn
{
    /// <summary>
    /// 文档的样式：包括项目编号，字体，颜色，对齐
    /// </summary>
    public class WordStyle
    {
        Word.Document doc;
        public WordStyle(Word.Document doc)
        {
            this.doc = doc;
        }

        public void GetAllStyle(Message msg)
        {
            foreach (Word.Paragraph item in doc.Paragraphs)
            {
                msg.AddTemplateMsg(item.Range);
            }
        }

        public void SetExsitsStyle(Message msg)
        {
            Object docStyle = null;
            foreach (Word.Paragraph item in doc.Paragraphs)
            {
                if (item.Range.Text.Trim() == "项目1")
                {
                    docStyle = item.get_Style();//获取文字为项目1的样式
                }
            }

            doc.Paragraphs.Last.Range.InsertParagraphAfter();
            Word.Range r = doc.Paragraphs.Last.Range.Duplicate;
            r.Text = "The Same AS \"项目1\" Style";
            //object strStyle = "标题 2"; //或者直接写一个已经存在的样式
            r.set_Style(ref docStyle);
            msg.AddTemplateMsg(r);
        }

        public void SetSymbolStyle(Message msg)
        {
            //项目符号样式
            Word.Range r = doc.Paragraphs.Last.Range.Duplicate;
            r.Start = r.End;//调整范围不影响上一段落 很关键这句
            r.InsertParagraphAfter();


            r.InsertAfter("This is Line One With Symbol");
            r.InsertParagraphAfter();
            r.InsertAfter("This is Line Two With Symbol");

            //wdBulletGallery 1项目符号列表; wdNumberGallery 2编号列表; wdOutlineNumberGallery  3多级符号列表
            Word.ListTemplate temp = doc.Application.ListGalleries[Word.WdListGalleryType.wdBulletGallery].ListTemplates[1]; //1实心圆圈 2实心方 3 自己查...
            object bContinuePreList = false;
            object applyTo = Word.WdListApplyTo.wdListApplyToWholeList;
            object defBehavior = Word.WdDefaultListBehavior.wdWord10ListBehavior;
            r.ListFormat.ApplyListTemplate(temp, ref bContinuePreList, ref applyTo, ref defBehavior);
            msg.AddTemplateMsg(r);
        }

        public void SetNumberStyle(Message msg)
        {
            //数字符号样式
            Word.Range r = doc.Paragraphs.Last.Range.Duplicate;
            r.Start = r.End;//调整范围不影响上一段落 很关键这句
            r.InsertParagraphAfter();
            r.InsertAfter("This is Line One With Number");
            r.InsertParagraphAfter();
            r.InsertAfter("This is Line Two With Number");

            Word.ListTemplate temp = doc.Application.ListGalleries[Word.WdListGalleryType.wdNumberGallery].ListTemplates[1];
            Word.ListLevel lev1 = temp.ListLevels[1];

            lev1.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
            lev1.NumberFormat = @"%1.";

            //lev.NumberPosition = 20;
            //  lev.Alignment = Word.WdListLevelAlignment.wdListLevelAlignCenter;
            //lev.TabPosition = 0;
            //lev.TextPosition = 0;
            //lev1.ResetOnHigher = 0;
            lev1.StartAt = 1;  //起始的数字
            object bContinuePreList = false;
            object applyTo = Word.WdListApplyTo.wdListApplyToWholeList;
            object defBehavior = Word.WdDefaultListBehavior.wdWord10ListBehavior;
            r.ListFormat.ApplyListTemplate(temp, ref bContinuePreList, ref applyTo, ref defBehavior);

            msg.AddTemplateMsg(r);
        }

        public void SetCustomStyle(Message msg)
        {
            Word.Range r = doc.Paragraphs.Last.Range.Duplicate;
            r.Start = r.End ;//调整范围不影响上一段落 很关键这句

            r.InsertParagraphAfter();
            r.InsertAfter("This is Line One With Custom");
            r.InsertParagraphAfter();
            r.InsertAfter("This is Line Two With Custom");

            var style = doc.Styles.Cast<Word.Style>().FirstOrDefault(s => s.NameLocal == "MyTestStyle") ?? doc.Styles.Add("MyTestStyle");
            style.set_BaseStyle(Word.WdBuiltinStyle.wdStyleListBullet); // msdn:https://docs.microsoft.com/zh-cn/office/vba/api/word.wdbuiltinstyle
            style.Font.Size = float.Parse("10.5");
            style.Font.Bold = 0;

            object obj = (object)style;
            r.set_Style(ref obj);//特别注意：这种设置样式的方式和ApplyListTemplate在边界范围上不一致，该方式有边界问题，更不能混合使用!!
            msg.AddTemplateMsg(r);
        }

        public void GetListValue(Message msg)
        {
            foreach (Word.Paragraph item in doc.Paragraphs)
            {
                msg.AddListValueMsg(item.Range);//对于项目符号样式会大于0，否则都等于0；如果是多个项目中的第一个就是1，第二个就是2.... 以此类推。
            }
        }

        public void SetIndent(Message msg)
        {
            Word.Range r = doc.Application.Selection.Range;
            r.ParagraphFormat.LeftIndent = 0;
            
            msg.AddMsg(r.Text+" 左侧缩进");
        }
    }
}
